﻿namespace DreamBuilder.Common
{
    using DreamBuilder.Core.Container;
    using DreamBuilder.Core.Message;
    using DreamBuilder.DictionaryAccess;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class BusinessBase : IBusiness
    {
        private IDataAccess dataAccess;

        public virtual ResultInfo CheckBusinessKeyUniqueRestrict(DataSet dsInfo)
        {
            ResultInfo info = new ResultInfo();
            foreach (DataTable table in dsInfo.Tables)
            {
                string tableName = table.TableName;
                string columnName = table.PrimaryKey[0].ColumnName;
                string businessKey = DictionaryAccessFacade.GetTableDicInfo(tableName).BusinessKey;
                if (!string.IsNullOrEmpty(businessKey))
                {
                    string[] sUQFields = businessKey.Split(",".ToCharArray());
                    int length = sUQFields.Length;
                    foreach (DataRow row in table.Rows)
                    {
                        string publicString;
                        StringBuilder builder;
                        string str5;
                        string[] strArray2;
                        int num3;
                        if (row.RowState == DataRowState.Deleted)
                        {
                            continue;
                        }
                        object[] sDataValues = new object[length];
                        for (int i = 0; i < sUQFields.Length; i++)
                        {
                            if ((sUQFields[i] != null) && (sUQFields[i].Trim() != ""))
                            {
                                sDataValues[i] = row[tableName + "." + sUQFields[i]];
                            }
                        }
                        switch (Convert.ToInt32(row["RowStatus"].ToString().Trim()))
                        {
                            case 1:
                                if (!this.DataAccess.CheckFieldValueExist(tableName, sUQFields, sDataValues))
                                {
                                    goto Label_0312;
                                }
                                info.bSuccess = false;
                                info.ErrorTable = tableName;
                                info.ErrorField = sUQFields[0];
                                publicString = CResManager.GetPublicString("FrameError", "ERRDAE06");
                                builder = new StringBuilder();
                                strArray2 = sUQFields;
                                num3 = 0;
                                goto Label_01CC;

                            case 2:
                                if (!this.DataAccess.CheckFieldValueExistExceptTheValue(tableName, sUQFields, sDataValues, columnName, row[columnName].ToString().Trim()))
                                {
                                    goto Label_0312;
                                }
                                info.bSuccess = false;
                                info.ErrorTable = tableName;
                                info.ErrorField = sUQFields[0];
                                publicString = CResManager.GetPublicString("FrameError", "ERRDAE06");
                                builder = new StringBuilder();
                                strArray2 = sUQFields;
                                num3 = 0;
                                goto Label_02BA;

                            default:
                                goto Label_0312;
                        }
                    Label_01A9:
                        str5 = strArray2[num3];
                        builder.Append(str5 + ",");
                        num3++;
                    Label_01CC:
                        if (num3 < strArray2.Length)
                        {
                            goto Label_01A9;
                        }
                        builder.Remove(builder.Length - 1, 1);
                        info.ErrorDesc = string.Format(publicString, (object[]) new string[] { builder.ToString() });
                        info.ErrorResID = sUQFields.ToString();
                        return info;
                    Label_0297:
                        str5 = strArray2[num3];
                        builder.Append(str5 + ",");
                        num3++;
                    Label_02BA:
                        if (num3 < strArray2.Length)
                        {
                            goto Label_0297;
                        }
                        builder.Remove(builder.Length - 1, 1);
                        info.ErrorDesc = string.Format(publicString, (object[]) new string[] { builder.ToString() });
                        info.ErrorResID = sUQFields.ToString();
                        return info;
                    Label_0312:;
                    }
                }
            }
            return info;
        }

        public virtual ResultInfo CheckForeignKeyRestrict(DataSet dsInfo)
        {
            ResultInfo info = new ResultInfo();
            foreach (DataTable table in dsInfo.Tables)
            {
                string tableName = table.TableName;
                string columnName = table.PrimaryKey[0].ColumnName;
                DataTable restrictFKSInfo = this.DataAccess.GetRestrictFKSInfo(tableName);
                if (restrictFKSInfo == null)
                {
                    info.bSuccess = true;
                    return info;
                }
                foreach (DataRow row in restrictFKSInfo.Rows)
                {
                    string sTableCode = row["RefKeyTableName"].ToString();
                    string str4 = row["RefKeyColumnName"].ToString();
                    foreach (DataRow row2 in table.Rows)
                    {
                        if (row2.RowState == DataRowState.Deleted)
                        {
                            continue;
                        }
                        if (Convert.ToInt32(row2["RowStatus"].ToString().Trim()) != 3)
                        {
                            object obj2 = row2[table.TableName + "." + row["OwnKeyName"].ToString()];
                            if (obj2 != DBNull.Value)
                            {
                                info.bSuccess = false;
                                string publicString = CResManager.GetPublicString("FrameError", "ERRDAE05");
                                info.ErrorDesc = string.Format(publicString, (object[]) new string[] { tableName, str4 });
                                info.ErrorTable = tableName;
                                info.ErrorField = str4;
                                info.ErrorResID = sTableCode + "__" + str4;
                                if (dsInfo.Tables[sTableCode] != null)
                                {
                                    foreach (DataRow row3 in dsInfo.Tables[sTableCode].Rows)
                                    {
                                        if (obj2 == row3[dsInfo.Tables[sTableCode].TableName + "." + str4])
                                        {
                                            info.bSuccess = true;
                                            break;
                                        }
                                    }
                                }
                                if (!info.bSuccess)
                                {
                                    string[] sUQFields = new string[1];
                                    object[] sDataValues = new object[1];
                                    sUQFields[0] = str4;
                                    sDataValues[0] = obj2;
                                    if (this.DataAccess.CheckFieldValueExist(sTableCode, sUQFields, sDataValues))
                                    {
                                        info.bSuccess = true;
                                    }
                                }
                                if (!info.bSuccess)
                                {
                                    return info;
                                }
                            }
                        }
                    }
                }
            }
            return info;
        }

        public virtual ResultInfo CheckLogic(DataSet dsInfo)
        {
            return new ResultInfo { bSuccess = true };
        }

        public virtual ResultInfo CheckUniqueRestrict(DataSet dsInfo)
        {
            ResultInfo info = new ResultInfo();
            foreach (DataTable table in dsInfo.Tables)
            {
                string tableName = table.TableName;
                string columnName = table.PrimaryKey[0].ColumnName;
                DataTable restrictUQInfoByTable = this.DataAccess.GetRestrictUQInfoByTable(tableName);
                if (restrictUQInfoByTable == null)
                {
                    info.bSuccess = true;
                    return info;
                }
                string str3 = ";";
                foreach (DataRow row in restrictUQInfoByTable.Rows)
                {
                    string str4 = row["UniqueName"].ToString();
                    if (str3.IndexOf(";" + str4 + ";") < 0)
                    {
                        str3 = str3 + str4 + ";";
                        string[] sUQFields = new string[] { row["ColName"].ToString() };
                        foreach (DataRow row2 in table.Rows)
                        {
                            string publicString;
                            object[] sDataValues = new object[1];
                            if ((sUQFields[0] != null) && (sUQFields[0].Trim() != ""))
                            {
                                sDataValues[0] = row2[sUQFields[0]];
                            }
                            switch (Convert.ToInt32(row2["RowStatus"].ToString().Trim()))
                            {
                                case 1:
                                    if (!this.DataAccess.CheckFieldValueExist(tableName, sUQFields, sDataValues))
                                    {
                                        break;
                                    }
                                    info.bSuccess = false;
                                    info.ErrorTable = tableName;
                                    info.ErrorField = sUQFields[0];
                                    publicString = CResManager.GetPublicString("FrameError", "ERRDAE06");
                                    info.ErrorDesc = string.Format(publicString, (object[]) new string[] { str4 });
                                    info.ErrorResID = str4;
                                    return info;

                                case 2:
                                    if (!this.DataAccess.CheckFieldValueExistExceptTheValue(tableName, sUQFields, sDataValues, columnName, row2[columnName].ToString().Trim()))
                                    {
                                        break;
                                    }
                                    info.bSuccess = false;
                                    info.ErrorTable = tableName;
                                    info.ErrorField = sUQFields[0];
                                    publicString = CResManager.GetPublicString("FrameError", "ERRDAE06");
                                    info.ErrorDesc = string.Format(publicString, (object[]) new string[] { str4 });
                                    info.ErrorResID = str4;
                                    return info;
                            }
                        }
                    }
                }
            }
            return info;
        }

        public virtual ResultInfo CheckValueRefByOtherTable(DataSet dsInfo)
        {
            ResultInfo info = new ResultInfo();
            foreach (DataTable table in dsInfo.Tables)
            {
                string tableName = table.TableName;
                string columnName = table.PrimaryKey[0].ColumnName;
                DataTable fKRefByOtherTables = this.DataAccess.GetFKRefByOtherTables(tableName);
                if (fKRefByOtherTables == null)
                {
                    info.bSuccess = true;
                    return info;
                }
                foreach (DataRow row in fKRefByOtherTables.Rows)
                {
                    string sTableCode = row["UseRefTableName"].ToString();
                    string str4 = row["UseRefColumnName"].ToString();
                    string[] sUQFields = new string[] { str4 };
                    object[] sDataValues = new object[1];
                    foreach (DataRow row2 in table.Rows)
                    {
                        if (row2.RowState == DataRowState.Deleted)
                        {
                            continue;
                        }
                        if (Convert.ToInt32(row2["RowStatus"].ToString().Trim()) == 3)
                        {
                            sDataValues[0] = row2[columnName];
                            if ((sDataValues[0] != DBNull.Value) && this.DataAccess.CheckFieldValueExist(sTableCode, sUQFields, sDataValues))
                            {
                                info.bSuccess = false;
                                info.ErrorTable = tableName;
                                string publicString = CResManager.GetPublicString("FrameError", "ERRDAE04");
                                info.ErrorDesc = string.Format(publicString, (object[]) new string[] { sTableCode, str4 });
                                info.ErrorResID = sTableCode + "__" + str4;
                                DataTable table3 = dsInfo.Tables[sTableCode];
                                if (table3 != null)
                                {
                                    foreach (DataRow row3 in table3.Rows)
                                    {
                                        if (row3[str4].ToString() == row2[columnName].ToString())
                                        {
                                            info.bSuccess = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        if (!info.bSuccess)
                        {
                            return info;
                        }
                    }
                }
            }
            return info;
        }

        public virtual ResultInfo Delete(List<IDeleteData> deleteList)
        {
            ResultInfo info = this.DeleteCheck(deleteList);
            if (info.bSuccess)
            {
                try
                {
                    this.DataAccess.DeleteData(deleteList);
                    info.bSuccess = true;
                }
                catch (SqlException exception)
                {
                    info.bSuccess = false;
                    info.ErrorID = exception.Number.ToString();
                }
                catch (Exception exception2)
                {
                    info.bSuccess = false;
                    info.ErrorDesc = exception2.Message;
                }
            }
            return info;
        }

        public virtual ResultInfo Delete(DataSet dsInfo)
        {
            ResultInfo info = this.DeleteCheck(dsInfo);
            if (info.bSuccess)
            {
                try
                {
                    this.DataAccess.DeleteData(dsInfo);
                    info.bSuccess = true;
                }
                catch (SqlException exception)
                {
                    info.bSuccess = false;
                    info.ErrorID = exception.Number.ToString();
                }
                catch (Exception exception2)
                {
                    info.bSuccess = false;
                    info.ErrorDesc = exception2.Message;
                }
            }
            return info;
        }

        public virtual ResultInfo DeleteCheck(List<IDeleteData> deleteList)
        {
            ResultInfo info = null;
            DataSet dsInfo = new DataSet();
            info = this.CheckValueRefByOtherTable(dsInfo);
            if (info.bSuccess && !info.bSuccess)
            {
                return info;
            }
            return info;
        }

        public virtual ResultInfo DeleteCheck(DataSet dsInfo)
        {
            ResultInfo info = null;
            info = this.CheckValueRefByOtherTable(dsInfo);
            if (info.bSuccess)
            {
                info = this.CheckLogic(dsInfo);
                if (!info.bSuccess)
                {
                    return info;
                }
            }
            return info;
        }

        public virtual ResultInfo Save(DataSet dsInfo)
        {
            ResultInfo info = this.SaveCheck(dsInfo);
            if (info.bSuccess)
            {
                try
                {
                    this.DataAccess.SaveData(dsInfo);
                    info.bSuccess = true;
                }
                catch (SqlException exception)
                {
                    info.bSuccess = false;
                    info.ErrorID = exception.Number.ToString();
                }
                catch (Exception exception2)
                {
                    info.bSuccess = false;
                    info.ErrorDesc = exception2.Message;
                }
            }
            return info;
        }

        public virtual ResultInfo SaveCheck(DataSet dsInfo)
        {
            ResultInfo info = null;
            try
            {
                info = this.CheckForeignKeyRestrict(dsInfo);
                if (!info.bSuccess)
                {
                    return info;
                }
                info = this.CheckBusinessKeyUniqueRestrict(dsInfo);
                if (!info.bSuccess)
                {
                    return info;
                }
                info = this.CheckValueRefByOtherTable(dsInfo);
                if (!info.bSuccess)
                {
                    return info;
                }
                info = this.CheckLogic(dsInfo);
                if (!info.bSuccess)
                {
                    return info;
                }
            }
            catch (SqlException exception)
            {
                info = new ResultInfo {
                    bSuccess = false,
                    ErrorID = exception.Number.ToString()
                };
            }
            catch (Exception exception2)
            {
                info = new ResultInfo {
                    bSuccess = false,
                    ErrorDesc = exception2.Message
                };
            }
            return info;
        }

        public IDataAccess DataAccess
        {
            get
            {
                if (this.dataAccess == null)
                {
                    this.dataAccess = UContainer.Resolve<IDataAccess>();
                }
                return this.dataAccess;
            }
            set
            {
                this.dataAccess = value;
            }
        }
    }
}

